package jpb.module.rest.profession;

import jpb.constant.Reply;
import jpb.constant.ReturnState;
import jpb.module.daomain.profession.Profession;
import jpb.module.service.profession.ProfessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by haol on 2017/4/5.
 */
@RestController
@RequestMapping("/api/web/profession")
public class ProfessionController {

        @Autowired
        private ProfessionService professionService;

        /**
         *@api {POST} /api/web/profession/i/oid 增加职称
         * @apiGroup Profession
         * @apiName add
         * @apiDescription  增加职称信息
         * @apiParam {String} name 职称名(not null)
         *  @apiParam {String} introduce 职称介绍
         *  @apiParamExample {json} 请求样例：
         *  POST /api/web/profession/i/organize_id
         *  {
         *          "name":"国家主席";
         *          "introduce":"负责国家大事",
         *  }
         *  @apiSuccessExample {json} 返回样例:
         *  {
         *      "code":0,"msg":"增加成功"
         *  }
         */
        @RequestMapping(value = "/i/{oid}", method = RequestMethod.POST)
        public String add(Profession profession) {
                Reply reply = null;
                Profession profession1 = professionService.get(profession.getOid(), profession.getName());
                if (profession1 != null) {
                        reply = new Reply(0, ReturnState.NO_UNIQUE);
                } else {
                        professionService.add(profession);
                        reply = new Reply(0, ReturnState.INSERT_SUCCESS);
                }
                return reply.toJson();
        }
        /**
         *@api {DELETE} /api/web/profession/i/{oid}/{id} 删除职称
         * @apiGroup Profession
         * @apiName delete
         * @apiDescription  删除职称id为{id}的职称
         *  @apiParamExample {json} 请求样例：
         *  DELETE /api/web/profession/i/organize_id/profession_id
         *  @apiSuccessExample {json} 返回样例:
         *  {
         *      "code":0,"msg":"删除成功"
         *  }
         */
        @RequestMapping(value = "/i/{oid}/{id}", method = RequestMethod.DELETE)
        public String delete(@PathVariable Integer id,@PathVariable String oid) {
                if (!professionService.checkIsExist(oid,id)){
                        return new Reply(1,ReturnState.NO_FIND).toJson();
                }
                professionService.delete(id);
                return new Reply(0,ReturnState.DELETE_SUCCESS).toJson();
        }
        /**
         *@api {POST} /api/web/profession/i/{oid}/{id} 更新职称
         * @apiGroup Profession
         * @apiName update
         * @apiDescription  更新职称id为{id}信息
         * * @apiParam {int} id 职称id(not null)
         * @apiParam {String} name 职称名(not null)
         *  @apiParam {String} introduce 职称介绍
         *  @apiParamExample {json} 请求样例：
         *  POST /api/web/profession/i/organize_id/profession_id
         *  {
         *            "id":"1",
         *          "name":"国家主席",
         *          "introduce":"负责国家大事",
         *  }
         *  @apiSuccessExample {json} 返回样例:
         *  {
         *      "code":0,"msg":"更新成功"
         *  }
         */
        @RequestMapping(value = "/i/{oid}/{id}", method = RequestMethod.POST)
        public String update(Profession profession) {
                if (!professionService.checkIsExist(profession.getOid(),profession.getId())){
                        return new Reply(1,ReturnState.NO_FIND).toJson();
                }
                Profession pro = professionService.get(profession.getOid(),profession.getName());
                if (pro == null && pro.getId() == profession.getId()){
                        return new Reply(1,ReturnState.NO_UNIQUE).toJson();
                }
                professionService.update(profession);
                return new Reply(0,ReturnState.UPDATE_SUCCESS).toString();
        }
        /**
         *@api {GET} /api/web/profession/i/{oid} 获取职称ByOid
         * @apiGroup Profession
         * @apiName getByOid
         * @apiDescription  获取组织id为{oid}的全部职称
         *  @apiParamExample {json} 请求样例：
         *  GET /api/web/profession/i/organize_id/profession_id
         *  @apiSuccessExample {json} 返回样例:
         *  {
         *  "code":0,"msg":"成功","data":[{"id":13,"name":"班长","introduce":"不填写"},{"id":14,"name":"团支书","introduce":"不填写"},{"id":15,"name":"团员","introduce":"不填写"},{"id":16,"name":"学生","introduce":"不填写"},{"id":17,"name":"学习委员","introduce":"不填写"}]
         *  }
         */
        @RequestMapping(value = "/i/{oid}", method = RequestMethod.GET)
        public String getByOid(@PathVariable String oid) {
                List<Profession> professions = professionService.get(oid);
                Reply<List<Profession>> reply = new Reply<List<Profession>>(0,ReturnState.SUCCESS,professions);

                return reply.toJson();
        }
        /**
         *@api {GET} /api/web/profession/i/{oid}/{id} 获取职称信息ById
         * @apiGroup Profession
         * @apiName get
         * @apiDescription  获取职称id为{id}的职称信息
         *  @apiParamExample {json} 请求样例：
         *  GET /api/web/profession/i/organize_id/profession_id
         *  @apiSuccessExample {json} 返回样例:
         *  {
         *      "code":0,"msg":"成功","data":{"id":13,"name":"班长","introduce":"不填写"}
         *  }
         */
        @RequestMapping(value = "/i/{oid}/{id}", method = RequestMethod.GET)
        public String get(@PathVariable Integer id) {
                Profession profession = professionService.get(id);

                Reply<Profession> reply = new Reply<Profession>(0,ReturnState.SUCCESS,profession);
                return reply.toJson();
        }


}
